﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace DeVerwachtingAmeland
{
    class Data_class
    {
        public const int WM_SETTEXT = 0x000C;
        public const int WM_GETTEXTLENGTH = 0x000E;
        public const int WM_GETTEXT = 0x000D; 

        /// <summary>
        /// The SendMessage API
        /// </summary>
        /// <param name="hWnd">handle to the required window</param>
        /// <param name="msg">the system/Custom message to send</param>
        /// <param name="wParam">first message parameter</param>
        /// <param name="lParam">second message parameter</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = System.Runtime.InteropServices.CharSet.Auto)] //
        public static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
        IntPtr hertzTextBoxHandle;
        IntPtr wattTextBoxHandle;
        /// <summary>
        /// The constructor of this class
        /// </summary>
        /// <param name="hertz"></param>
        /// <param name="watt"></param>
        public Data_class(IntPtr hertz, IntPtr watt)
        {
            this.hertzTextBoxHandle = hertz;
            this.wattTextBoxHandle = watt;
        }
        /// <summary>
        /// This method gets the current value of the hertz Textbox
        /// </summary>
        /// <returns></returns>
        public string getHertzInfo()
        {
            //place text in the power textbox
            //string msg = "pudding";
            //IntPtr text = System.Runtime.InteropServices.Marshal.StringToCoTaskMemUni(msg);
            //SendMessage((int)hwndNeighbour, WM_SETTEXT, 0, text);

            Int32 txtlen;
            txtlen = SendMessage((int)hertzTextBoxHandle, WM_GETTEXTLENGTH, 0, IntPtr.Zero);
            txtlen = txtlen + 1;
            StringBuilder txt = new StringBuilder(txtlen);
            SendMessage(hertzTextBoxHandle, WM_GETTEXT, txt.Capacity, txt);

            string hertz = txt.ToString();
            IntPtr textor = System.Runtime.InteropServices.Marshal.StringToCoTaskMemUni(hertz);
            return hertz;
        }
        /// <summary>
        /// This method gets the current value of the Watt Textbox
        /// </summary>
        /// <returns></returns>
        public string getWattInfo()
        {
            Int32 txtlen;
            txtlen = SendMessage((int)wattTextBoxHandle, WM_GETTEXTLENGTH, 0, IntPtr.Zero);
            txtlen = txtlen + 1;
            StringBuilder txt = new StringBuilder(txtlen);
            SendMessage(wattTextBoxHandle, WM_GETTEXT, txt.Capacity, txt);

            string watt = txt.ToString();
            IntPtr textor = System.Runtime.InteropServices.Marshal.StringToCoTaskMemUni(watt);
            return watt;
        }
        /// <summary>
        /// This method places info in the textboxes of Multitool
        /// </summary>
        public void setInfo()
        {
            //place text in the power textbox
            string msg = "22,5";
            IntPtr text = System.Runtime.InteropServices.Marshal.StringToCoTaskMemUni(msg);
            SendMessage((int)hertzTextBoxHandle, WM_SETTEXT, 0, text);
            SendMessage((int)wattTextBoxHandle, WM_SETTEXT, 0, text);
        }
    }
}
